package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.ServerExamData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildServerExamDaoImpl extends TempDataObjectDao<ServerExamData> {

    public static String getServerExamDataSql = "select * from `t_c_grand_child_server_exam`";

    public Map<Long, ServerExamData> getServerExamDataMap() {
        Map<Long, ServerExamData> data = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getServerExamDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerExamData parse = parse(rs);
                data.put(parse.getServerId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }

    public static String updateServerExamDataSql = "update `t_c_grand_child_server_exam` set examTimes=?,globalList=?,allExamTimes=?,allGlobalList=? where serverId=?";

    @Override
    protected boolean updateData(ServerExamData serverExamData) {
        serverExamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, serverExamData.getExamTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(serverExamData.getGlobalList()))));
        para.put(i++, new DbParameter(Types.BIGINT, serverExamData.getAllExamTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(serverExamData.getAllGlobalList()))));
        para.put(i++, new DbParameter(Types.BIGINT, serverExamData.getServerId()));
        if (getDb().executeNoneQuery(updateServerExamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertServerExamDataSql = "insert into `t_c_grand_child_server_exam`(serverId,examTimes,globalList,allExamTimes,allGlobalList) values (?,?,?,?,?)";

    @Override
    protected boolean addData(ServerExamData serverExamData) {
        serverExamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, serverExamData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, serverExamData.getExamTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(serverExamData.getGlobalList()))));
        para.put(i++, new DbParameter(Types.BIGINT, serverExamData.getAllExamTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(serverExamData.getAllGlobalList()))));
        if (getDb().executeNoneQuery(insertServerExamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ServerExamData parseData(ResultSet rs) throws SQLException {
        ServerExamData serverExamData = new ServerExamData();
        serverExamData.setServerId(rs.getLong("serverId"));
        serverExamData.setExamTimes(rs.getLong("examTimes"));
        String globalList = rs.getString("globalList");
        if (StringUtils.isNullOrEmpty(globalList)) {
            globalList = "[]";
        }
        serverExamData.setGlobalList(JSON.parseObject(globalList, new TypeReference<List<Long>>() {}));
        serverExamData.setAllExamTimes(rs.getLong("allExamTimes"));
        String allGlobalList = rs.getString("allGlobalList");
        if (StringUtils.isNullOrEmpty(allGlobalList)) {
            allGlobalList = "[]";
        }
        serverExamData.setAllGlobalList(JSON.parseObject(allGlobalList, new TypeReference<List<Long>>() {}));
        return serverExamData;
    }

}
